package com.eportal.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.eportal.aop.LogAnnotation;
import com.eportal.base.BaseDAO;
import com.eportal.domain.SysScoreRule;
import com.eportal.domain.Sysopt;
import com.eportal.service.SysScoreRuleService;
import com.eportal.util.ValidateUtil;
import com.framework.jqgrid.JqGridPager;

@Service("sysScoreRuleService")
public class SysScoreRuleServiceImpl  implements SysScoreRuleService{
	private Map<String, Object> result = new HashMap<String, Object>();
	private Logger log = Logger.getLogger(SysScoreRuleServiceImpl.class);
	@Resource(name="dao")
	private BaseDAO dao;
	
	public HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }
	@Override
	public SysScoreRule get(Long sid){
		SysScoreRule sysScoreRule = (SysScoreRule)dao.getById(SysScoreRule.class, sid);
		return sysScoreRule;
	}
	
	@Override
	public List<Map<String, Object>> list(JqGridPager jqGridPager) {
		 String sql = "select s.sid,s.incscore,m.mname from sys_score_rule s left join sysmenu m on s.murl=m.murl";
	     return dao.query(sql, jqGridPager);
	}
	
	@Override
	public void delete(Long id) {
		SysScoreRule sysScoreRule = (SysScoreRule) dao.getById(SysScoreRule.class, id);
        dao.delete(sysScoreRule);
	}
	@Override
	public void update(SysScoreRule sysScoreRule) throws Exception {
        dao.update(sysScoreRule);
	}
	
	@Override
	public void delete(List<Long> ids) {
		 for (Long id : ids) {
	            delete(id);
	     }
	}
	
	@Override
	public SysScoreRule add(SysScoreRule sysScoreRule) throws Exception {
		dao.save(sysScoreRule);
        return sysScoreRule;
	}

	/**
     * 积分规则新增保存
     */
    @LogAnnotation(operateDescribe="积分规则新增")  
    public Map<String, Object> addMethod(SysScoreRule sysScoreRule) {
    	 String msg = ValidateUtil.validate(sysScoreRule);
         if (msg == null || msg.trim().length() == 0) {
             try {
            	 Sysopt  user= (Sysopt)getSession().getAttribute("user");
            	 sysScoreRule.setcUserName(Long.toString(user.getSid()));
            	 sysScoreRule.setcTime(new Timestamp(System.currentTimeMillis()));
            	 add(sysScoreRule);
            	 result.put("objectId", sysScoreRule.getSid());
            	 result.put("code", 1);
                 result.put("msg", "操作成功");	
             } catch (Exception e) {
                 log.error("积分规则保存失败", e);
                 result.put("code", 0);
                 result.put("msg", "操作失败");
             }
         } else {
             result.put("code", 0);
             result.put("msg", msg);
         }
         return result;
    }
	/**
     * 积分规则修改保存
     */
    @LogAnnotation(operateDescribe="积分规则修改")  
    public Map<String, Object> updateMethod(SysScoreRule sysScoreRule){
    	 String msg = ValidateUtil.validate(sysScoreRule);
    	 result.put("objectId", sysScoreRule.getSid());
         if (msg == null || msg.trim().length() == 0) {
             try {
            	SysScoreRule entity = get(sysScoreRule.getSid());
            	entity.setmUrl(sysScoreRule.getmUrl());
             	entity.setIncScore(sysScoreRule.getIncScore());
             	Sysopt  user= (Sysopt)getSession().getAttribute("user");
            	 entity.setuUserName(Long.toString(user.getSid()));
            	 entity.setuTime(new Timestamp(System.currentTimeMillis()));
            	 update(entity);
                 result.put("code", 1);
                 result.put("msg", "操作成功");
             } catch (Exception e) {
                 log.error("积分规则更新失败", e);
                 result.put("code", 0);
                 result.put("msg", "操作失败");
             }
         } else {
             result.put("code", 0);
             result.put("msg", msg);
         }
         return result;
    	
    }


	/**
     * 删除
     */
    @LogAnnotation(operateDescribe="积分规则删除")  
    public Map<String, Object> deleteMethod(String param_ids) {
    	 try {
    		 result.put("objectId", param_ids);
             List<Long> ids = new LinkedList<Long>();
             for (String s : param_ids.split(",")) {
                 ids.add(Long.parseLong(s));
             }
             delete(ids);
             result.put("code", 1);
             result.put("msg", "操作成功");
         } catch (Exception e) {
             log.error("积分规则删除失败", e);
             result.put("code", 0);
             result.put("msg", "操作失败");
         }
        return result;
    }
    
	
}
